package ioperation;

import book.Book;
import book.Booklist;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘远望
 * Date: 2025-03-19
 * Time: 20:35
 */
public class DelOperation implements IOperation{
    @Override
    public void work(Booklist booklist) {
        System.out.println("删除图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入要删除的书名");
        String name = scanner.nextLine();
        int currentsize = booklist.getUseSize();
        int pos = -1;
        int i = 0;
        for( i = 0 ; i < currentsize ; i++){
            Book book = booklist.getBooks()[i];
            if(book.getName().equals(name)){
                pos = i;
                break;
            }
        }
        if(i == currentsize){
            System.out.println("没有要删除的书");
        }
        for (int j = pos; j < currentsize - 1; j++) {
            Book book = booklist.getBooks()[j + 1];
            booklist.setBook(j , book);
        }
        booklist.setBook(currentsize - 1, null);
        booklist.setUseSize(currentsize - 1);
        System.out.println("删除成功");
    }
}
